#drafted by Jeremy VanDerWal ( jjvanderwal@gmail.com ... www.jjvanderwal.com )
#GNU General Public License .. feel free to use / distribute ... no warranties

################################################################################
################################################################################
# script to extract data at dates and times of interest

################################################################################
# set the constants

#define some directories
work.dir = '/home1/55/jc224266/working/fire/fireasc/firebackground/'; setwd(work.dir)
data.dir = '/homes/31/jc165798/Climate/AWAP.direct.download/summaries/Oz/monthly.asc/'

#read in the position info for the data csv files
base.pos = read.csv('/homes/31/jc165798/Climate/AWAP.direct.download/summaries/Oz/base.positions.csv') #positions of data files
base.pos$LATDEC =  sprintf('%.2f',base.pos$lat); base.pos$LONGDEC = sprintf('%.2f',base.pos$lon)
base.pos$lat = base.pos$lon = base.pos$row = base.pos$col = NULL; base.pos$row = 1:nrow(base.pos)

#data file
#define and read in key files
bkgd.file = "bkgdNorthOZ.csv"
bkgd = read.csv(bkgd.file,stringsAsFactors=F)
bkgd$LATDEC = sprintf('%.2f',bkgd$LATDEC)
bkgd$LONGDEC = sprintf('%.2f',bkgd$LONGDEC)
bkgd$month = sprintf('%02d',bkgd$month)
bkgd = merge(bkgd,base.pos)
# done already bkgd$tfile = paste(bkgd$YEAR,bkgd$MONTH,sep='')
bkgd = bkgd[order(bkgd$tfile),]
bkgd = na.omit (bkgd)

# fire occurance file
occurdir =  "/home1/55/jc224266/working/fire/"
base.occur.file = 'fire00_09.csv'; occur = read.csv(paste(occurdir, base.occur.file,sep =""),stringsAsFactors=F)
occur$species = NULL
occur$LATDEC = sprintf('%.2f',occur$LATDEC); occur$LONGDEC = sprintf('%.2f',occur$LONGDEC); occur$month = sprintf('%02d',occur$month)
occur$tfile = paste(occur$year, occur$month, sep = '')
occur$X = NULL
names(occur) = c("LATDEC", "LONGDEC", "month", "year", "tfile")
occur = merge (occur, base.pos)
occur = occur [order(occur$tfile),]
occur = na.omit(occur)
occur$month = as.numeric (occur$month)
occur$month = sprintf('%02d',occur$month)
# break it down..

bkgd1 = subset (occur, month == 01)

################################################################################

### first extract the background information
#cycle through each of hte csv datafiles
for (tfile in unique(bkgd$tfile)){
	if (file.exists(paste(data.dir,tfile,'/data.csv',sep=''))) {
		cat(tfile,'\n')
		#read in the csv.data file
		csv.data = read.csv(paste(data.dir,tfile,'/data.csv',sep=''),as.is=T)
		#setup the bkgd data if first file
		if (length(bkgd)<10) { for (ii in 5:26) { bkgd[names(csv.data)[ii]] = NA } }
		#get the rows in bkgd
		bkgd.rows = which(bkgd$tfile==tfile); bkgd.cols = 7:26
		csv.rows = bkgd$row[bkgd.rows]; csv.cols = 5:24
		#copy the data
		bkgd[bkgd.rows,bkgd.cols] = csv.data[csv.rows,csv.cols]
	}
 }

nrow(bkgd);bkgd = na.omit(bkgd); nrow(bkgd)#remove the NA data

#merge bkgd with occur information
#occur = merge(occur,bkgd)


#write out the appropriately structured files for maxent
bkgd = data.frame(species = 'bkgd', bkgd)
bkgd$month = bkgd$year = bkgd$row = bkgd$tfile = bkgd$row = NULL
write.csv(bkgd,'fire.bkgd.tfile.csv',row.names=F,na='')

occur1 = occur
occur1$species = "fire"
namesind=function(df){

    temp1=names(df)
    temp2=seq(1,length(temp1))
    temp3=data.frame(temp1,temp2)
    names(temp3)=c("VAR","COL")
    return(temp3)
    rm(temp1,temp2,temp3)
}

ni <- namesind

ni(occur1)

write.csv(occur,'fire.occurMonthYear.csv',row.names=F,na='')
write.csv(occur1[,c(27,1,2,7:26)],'fire00_09.occur.csv',row.names=F,na='')

occur2 = occur1 [,c(27,1,2,7:26)]



#bkgd = read.csv ("fire.bkgd_month_year_tfile.csv")
#save(bkgd, file = "firebkgd_tfile.RData" )



         
############################################################################################
######### now append the static data into the bkgd and occur files   #######################
############################################################################################ 

#bkgd - load in the file and do stuff to it

load ("firebkgd_tfile.RData")

GIS.dir = "/home1/55/jc224266/working/fire/fireasc/northozveg/" 
lat.col = 'LATDEC'
long.col= 'LONGDEC'

library (SDMTools)

#define todays data
t.date = format(Sys.time(),'%d%b%Y')

#get a list of .asc to append to table

GIS.layers = list.files(GIS.dir,pattern='\\.asc')

#store the gis data in a dataframe
GIS.data = positions = NULL
for (GIS in GIS.layers) {
  print(GIS)  
  tasc = round(import.asc(paste(GIS.dir,GIS,sep='')),0) #read in the layer
  if (length(positions)==0 & length(GIS.data)==0) { #if there is no data stored yet, get it
    positions = which (!is.na(tasc))
    GIS.data = data.frame(tasc[positions])
    names(GIS.data) = gsub('\\.asc','',GIS.layers[1])
  } else { #if dataset already started... simply append data
    GIS.data[[gsub('\\.asc','',GIS)]] = tasc[positions]  
  }
  #append the location specific data to the bkgd table
  bkgd[[gsub('\\.asc','',GIS)]] = join.asc(data.frame(X=bkgd[[long.col]],Y=bkgd[[lat.col]]),tasc)
}

bkgd$northozveg5km[is.na(bkgd$northozveg5km)] <- "0"  # change areas without values (NA) to 0

# subset the data remove unwanted columns and save out for maxent.

month = 1:12

for (mm in month){
     tdata = subset (bkgd, month == mm)
     tdata$year = tdata$month = tdata$tfile = tdata$row = NULL
        write.csv (tdata, (paste("bkgd", mm,".csv", sep = "")), row.names = F)
                }
############################################################################################                
#############################################################################################
# same thing for occur

occur = read.csv ("fire.occurMonthYear.csv")

GIS.dir = "/home1/55/jc224266/working/fire/fireasc/northozveg/" 
lat.col = 'LATDEC'
long.col= 'LONGDEC'

library (SDMTools)

#define todays data
t.date = format(Sys.time(),'%d%b%Y') 

#get a list of .asc 

GIS.layers = list.files(GIS.dir,pattern='\\.asc')

#store the gis data in a dataframe

GIS.data = positions = NULL
for (GIS in GIS.layers) {
  print(GIS)  
  tasc = round(import.asc(paste(GIS.dir,GIS,sep='')),0) #read in the layer
  if (length(positions)==0 & length(GIS.data)==0) { #if there is no data stored yet, get it
    positions = which (!is.na(tasc))
    GIS.data = data.frame(tasc[positions])
    names(GIS.data) = gsub('\\.asc','',GIS.layers[1])
  } else { #if dataset already started... simply append data
    GIS.data[[gsub('\\.asc','',GIS)]] = tasc[positions]  
  }
  #append the location specific data to the bkgd table
  occur[[gsub('\\.asc','',GIS)]] = join.asc(data.frame(X=occur[[long.col]],Y=occur[[lat.col]]),tasc)
}

occur$northozveg5km[is.na(occur$northozveg5km)] <- "0"
dim (occur)
write.csv (occur, "fire.occurMnthYrTfileVeg.csv"  # write out all the data for future subsets.

# subset the data remove unwanted columns and save out for maxent.

month = 1:12

for (mm in month){
     tdata = subset (occur, month == mm)
     tdata$year = tdata$month = tdata$tfile = tdata$row = NULL
        write.csv (tdata, (paste("occur", mm,".csv", sep = "")), row.names = F)
                 }